package com.mc.read.pcApi.controller;

import com.mc.read.common.emun.ServiceCodeEnum;
import com.mc.read.common.exception.ServiceException;
import com.mc.read.common.util.OSSClientUtil;
import com.mc.read.dao.mapper.UserinformationMapper;
import com.mc.read.dao.models.Userinformation;
import com.mc.read.dao.vo.FIleVo;
import com.mc.read.vo.respose.BaseRespose;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping(value = "file/")
@Slf4j
public class FileController {
    @Autowired
    UserinformationMapper userinformationMapper;

    /**
     * @Author:LiuHao
     * @Date:13:57 2018/7/2
     * 图片上传 addPic
     **/
    @ResponseBody
    @RequestMapping(value = "addPic", method = RequestMethod.POST)
    public BaseRespose addPic(@RequestParam("pic")MultipartFile pic, Integer userId, String token) {
        Userinformation userinformation = userinformationMapper.selectByPrimaryKey(userId);
        if(ObjectUtils.isEmpty(token)|| ObjectUtils.isEmpty(userId)){
            throw new ServiceException(ServiceCodeEnum.SIGN_IS_FAILURE);
        }
        if (ObjectUtils.isEmpty(userinformation)) {
            throw new ServiceException(300, "未找到该用户");
        }
        if (-1 == userinformation.getStaus()) {
            throw new ServiceException(300, "该用户已被禁用");
        }
        if (!(token.equals(userinformation.getToken()))) {
            throw new ServiceException(300, "该账号疑在另一设备登录,为确保安全");
        }
        BaseRespose baseRespose = new BaseRespose();
        baseRespose.setReturnString("");
        if (ObjectUtils.isEmpty(pic)) {

            baseRespose.setCode(800);
            baseRespose.setDisp("不能上传空文件");
            return baseRespose;
        }
        try {
            if (pic.getSize() > 1024 * 1024 * 5) {
                baseRespose.setCode(800);
                baseRespose.setDisp("单张图片或文件大小不能超过5MB");
                return baseRespose;
            }
            String url = "";
            if (pic != null) {
                String originalFilename = pic.getOriginalFilename();
                if (originalFilename != null && originalFilename.length() > 0) {
                    OSSClientUtil ossClientUtil = new OSSClientUtil();
                    url = ossClientUtil.uploadImg2Oss(pic);
                    log.info("上传文件成功,地址为" + url);
                }
            }
            FIleVo fIleVo = new FIleVo();
            fIleVo.setUrl(url);
            baseRespose.setReturnString(fIleVo);

        } catch (Exception e) {
            e.printStackTrace();
            baseRespose.setCode(800);
            baseRespose.setDisp("上传文件出错，请重新上传");
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:13:57 2018/7/2
     * 文件上传 addFile
     **/

    @ResponseBody
    @RequestMapping(value = "addFile", method = RequestMethod.POST)
    public BaseRespose addFile(@RequestParam(value="pic",required=false)MultipartFile pic) {
        BaseRespose baseRespose = new BaseRespose();
        baseRespose.setReturnString(pic.getName());

        return baseRespose;
    }

}
